part of 'widgets.dart';

/// 一块可以拖入文件的区域，本质上是一个Container
/// 当有文件拖入时，会变颜色
class LxDragArea extends StatefulWidget {
  final void Function(String) onDrag;

  final double? width;
  final double? height;

  /// 当拖动东西进去的时候，显示的颜色
  final Color color;
  final Widget? child;

  const LxDragArea({
    required this.onDrag,
    this.color = COLOR.white,
    this.width,
    this.height,
    this.child,
    super.key,
  });

  @override
  State<LxDragArea> createState() => _LxDragAreaState();
}

class _LxDragAreaState extends State<LxDragArea> {
  bool isDragging = false;

  @override
  Widget build(BuildContext context) {
    final Color background = isDragging
        ? widget.color.withOpacity(0.2)
        : widget.color.withOpacity(0.1);
    final Color border = isDragging ? widget.color : COLOR.transparent;

    return DropTarget(
      onDragDone: (detail) {
        String path = detail.files.first.path;
        // 如果目标是文件夹，则给path加一个后斜杠，和文件有所区分
        if (path.isDirectory) path += "/";

        widget.onDrag(path);
      },
      onDragEntered: (_) => setState(() {
        isDragging = true;
      }),
      onDragExited: (_) => setState(() {
        isDragging = false;
      }),
      child: Container(
        width: widget.width,
        height: widget.height,
        decoration: BoxDecoration(
          color: background,
          borderRadius: BorderRadius.circular(5),
          border: Border.all(
            width: 2,
            color: border,
          ),
        ),
        child: widget.child,
      ),
    );
  }
}
